using System;
using System.Collections.Generic;
using System.Text;
using ImagesMath;

namespace Graphics.Geometries
{
    public class Cube : Form
    {

        public Cube()
            : base()
        {

        }

        public override bool FirstIntersection( ref Ray  ray,ref Intersection  inter )
        {
        	
            
            double t ;
            inter.T = 1e30;
            PR3 p ;
            bool Intersection = false;

            VR3 direction = ray.Direction;
            PR3 origine = ray.Origin;

            if ((direction.Z > ImageMath.EPS) || (direction.Z < -ImageMath.EPS))
            {
                t = - (origine.Z - 0.5) / direction.Z ;
                p = ray.ExtractPosition (t) ;
                if ((Math.Abs(p.X) <= 0.5) && (Math.Abs(p.Y) <= 0.5) && (t < inter.T) && (t > ImageMath.EPS))
                {
                    inter.T = t ;
                    normale = new VR3 ( 0,0,1 ) ;
                    Intersection = true;
                }

                t = - (origine.Z + 0.5) / direction.Z ;
                p = ray.ExtractPosition (t) ;
                if ((Math.Abs(p.X) <= 0.5) && (Math.Abs(p.Y) <= 0.5) && (t < inter.T) && (t > ImageMath.EPS))
                {
                    inter.T = t ;
                    normale = new VR3 ( 0,0,-1 ) ;
                    Intersection = true;
                }
            }

            if ((direction.Y > ImageMath.EPS) || (direction.Y < -ImageMath.EPS))
            {
                t = - (origine.Y - 0.5) / direction.Y ;
                p = ray.ExtractPosition (t) ;
                if ((Math.Abs(p.X) <= 0.5) && (Math.Abs(p.Z) <= 0.5) && (t < inter.T) && (t > ImageMath.EPS))
                {
                    inter.T = t ;
                    normale = new VR3 ( 0,1,0 ) ;
                    Intersection = true;
                }
        		
                t = - (origine.Y + 0.5) / direction.Y ;
                p = ray.ExtractPosition (t) ;
                if ((Math.Abs(p.X) <= 0.5) && (Math.Abs(p.Z) <= 0.5) && (t < inter.T) && (t > ImageMath.EPS))
                {
                    inter.T = t ;
                    normale = new VR3 ( 0,-1,0 ) ;
                    Intersection = true;
                }
            }

            if ((direction.X > ImageMath.EPS) || (direction.X < -ImageMath.EPS))
            {
                t = - (origine.X - 0.5) / direction.X ;
                p = ray.ExtractPosition (t) ;
                if ((Math.Abs(p.Z) <= 0.5) && (Math.Abs(p.Y) <= 0.5) && (t < inter.T) && (t > ImageMath.EPS))
                {
                    inter.T = t ;
                    normale = new VR3 ( 1,0,0 ) ;
                    Intersection = true;
                }

                t = - (origine.X + 0.5) / direction.X ;
                p = ray.ExtractPosition (t) ;
                if ((Math.Abs(p.Z) <= 0.5) && (Math.Abs(p.Y) <= 0.5) && (t < inter.T) && (t > ImageMath.EPS))
                {
                    inter.T = t ;
                    normale = new VR3 ( -1,0,0 ) ;
                    Intersection = true;
                }
            }
        		
            return Intersection ;

        }

        public override VR3 Normal(PR3 p, ref Ray ray)
        {
            return normale ;
        }

            private VR3 normale;	
    }
}
